#!/bin/bash
# Precompile automatic libraries into byte-codes and pack into a single file

RULESFILE="${SINGLOGHOME}/auto.sl"
if [ ${1} ]; then
	RULESFILE="${1}"
fi

# Delete auto.slb first to start with a clean compile
PRECOMPILEDFILE="${RULESFILE/%\.sl/.slb}"
rm "${PRECOMPILEDFILE}"

# Compile all default rules into a file
"${SINGLOGHOME}/sl" $@ << IN
?
stream.writeFile .stream "${PRECOMPILEDFILE}",
(
	-- Write the rules first
	rule .rule,
	stream.dump .stream .rule,
	stream.put .stream " #%0A",
	fail
;
	-- Write the compiled stuff as set byte-code statement
	rule .rule,
	rule.headTail .rule .head .tail
	,
	-- Do not compile closures etc
	(not (is.tuple .head); nth .head 0 .first, is.atom .first)
	,
	head.callSign .head .sign,
	not (get COMPILED .sign),
	is.compiled .head, -- Only puts already compiled stuff, do not excess
	rule.callSign .proto .sign,
	bytecode.compile .proto .code,
	stream.put .stream ":- ",
	stream.dump .stream (set.bytecode .proto .code),
	stream.put .stream " #%0A%0A",
	set COMPILED .sign,
	fail
;
	-- Enables byte-code execution anyway
	stream.put .stream ":- enable.bytecode Y #",
	stream.close .stream
)
#
IN
